package com.jianzong.lingxuan.base;

import com.jianzong.lingxuan.contst.GlobalConsts;
import com.jianzong.lingxuan.enums.SysInfo;
import com.jianzong.lingxuan.error.IError;
import com.jianzong.lingxuan.error.SysError;
import lombok.Data;
import lombok.Getter;
import org.slf4j.MDC;
import org.springframework.data.domain.Page;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @auth Leiwenlong
 * @since 2021/7/19
 */
@Getter
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 5245758467689718735L;

    public Result() {
    }


    private int code;
    private String message;
    private Object data;
    private String seqid;

    protected Result(Builder builder) {
        this.code = builder.code;
        this.message = builder.message;
        this.data = builder.data;
        this.seqid = MDC.get(GlobalConsts.TRACE_KEY);
    }

    public static Builder builder() {
        return new Builder();
    }

    public static class Builder {
        private int code = GlobalConsts.SUCCESS_CODE;
        private String message = GlobalConsts.SUCCESS_MSG;
        private Object data;

        public Builder code(int code) {
            this.code = code;
            return this;
        }

        public Builder message(String message) {
            this.message = message;
            return this;
        }

        public Builder data(Object data) {
            this.data = data;
            return this;
        }

        public Result create() {
            return new Result(this);
        }
    }

    public static Result success() {
        return builder().create();
    }

    public static Result success(Object data) {
        return builder().data(data).create();
    }

    public static Result error(int code, String message) {
        return builder().code(code).message(message).create();
    }

    public static Result error(int code, String message, Object object) {
        return builder().code(code).message(message).data(object).create();
    }

    public static Result error(String message) {
        return builder().code(SysError.SYSTEM_ERROR.getCode()).message(message).create();
    }

    public static Result error() {
        return builder().code(SysError.PARAM_UNVALID.getCode()).message(SysError.PARAM_UNVALID.getMessage()).create();
    }

    public static Result error(SysInfo info) {
        return builder().code(info.getCode()).message(info.getMessage()).create();
    }

    public static Result error(IError err) {
        return error(err.getCode(), err.getMessage());
    }
}
